package com.beetlestech.nfe.main.biz.com.impl;

import java.util.UUID;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbEmailRegister;
import com.beetlestech.nfe.main.biz.com.HbSendHomeWorkToStudent;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.EmailactiveDTO;
import com.beetlestech.nfe.main.db.tbl.HomeworkhistoryDTO;
import com.beetlestech.nfe.main.db.tbl.MststudentDTO;
import com.beetlestech.nfe.main.db.tbl.MstteacherDTO;
import com.beetlestech.nfe.main.db.tbl.MstusrDTO;
import com.beetlestech.nfe.main.db.tbl.StudenthomeworkDTO;
import com.beetlestech.nfe.main.db.tbl.UsermessageDTO;

public class HbSendHomeWorkToStudentImpl extends AbstractDBLogic implements HbSendHomeWorkToStudent, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {

	@Override
	public void SendHomeWorkToStudent(String actionType, Integer userId,
			Integer homeWorkId, Integer sendId, Integer fileId, String userIdSql) {
		    if(actionType.equals("single")){
		    	if(checkHomeWorkIsExits(userId,homeWorkId)){
		    		StudenthomeworkDTO studenthomeworkDTO =new StudenthomeworkDTO();
		    		studenthomeworkDTO.setusrid(userId);
		    		studenthomeworkDTO.sethomeworkid(homeWorkId);
		    		studenthomeworkDTO.setsenderid(sendId);
		    		studenthomeworkDTO.setsenddate(getCurrentDate());
		    		studenthomeworkDTO.setsendtime(getCurrentTime());
		    		studenthomeworkDTO.setstate(0);
		    		updateDAO.updateOne(StudenthomeworkDTO.INSERT,studenthomeworkDTO,M10040WS);
		    		HomeworkhistoryDTO homeworkhistoryDTO=new HomeworkhistoryDTO();
		    		homeworkhistoryDTO.sethomeworkid(homeWorkId);
		    		homeworkhistoryDTO.setsenderid(sendId);
		    		homeworkhistoryDTO.setreceiverid(userId);
		    		homeworkhistoryDTO.setfileid(fileId);
		    		homeworkhistoryDTO.setstate(0);
		    		updateDAO.updateOne(HomeworkhistoryDTO.INSERT,homeworkhistoryDTO,M10040WS);
		    		UsermessageDTO usermessageDTO =new UsermessageDTO();
		    		MstteacherDTO mstteacherDTO =new MstteacherDTO();
		    		mstteacherDTO.setusrid(sendId);
		    		MstteacherDTO teacher=queryDAO.queryForObject(MstteacherDTO.EQUAL_SEARCH, mstteacherDTO);
		    		usermessageDTO.setTitel("您有一条"+teacher.getname()+"布置的作业");
		    		usermessageDTO.setMessage("老师");
		    		usermessageDTO.setSenderId(sendId);
		    		usermessageDTO.setReceiveId(userId);
		    		usermessageDTO.setSendDT(getCurrentDate());
		    		usermessageDTO.setSendTime(getCurrentTime());
		    		usermessageDTO.setReadflg(0);
		    		updateDAO.updateOne(UsermessageDTO.INSERT,usermessageDTO,M10040WS);
		    	}else{
		    		createApplicationException(M10105WC);
		    	}
		    	
		    }else if(actionType.equals("many")){
		    	int a=0;
		    	userIdSql=userIdSql.replace("(", "");
		    	userIdSql=userIdSql.replace(")", "");
		    	if(userIdSql.contains(",")){
		    		String[] userIdArray=userIdSql.split(",");
		    		for(int i=0;i<userIdArray.length;i++){
		    			if(checkHomeWorkIsExits(Integer.parseInt(userIdArray[i]),homeWorkId)){
				    		StudenthomeworkDTO studenthomeworkDTO =new StudenthomeworkDTO();
				    		studenthomeworkDTO.setusrid(Integer.parseInt(userIdArray[i]));
				    		studenthomeworkDTO.sethomeworkid(homeWorkId);
				    		studenthomeworkDTO.setsenderid(sendId);
				    		studenthomeworkDTO.setsenddate(getCurrentDate());
				    		studenthomeworkDTO.setsendtime(getCurrentTime());
				    		studenthomeworkDTO.setstate(0);
				    		updateDAO.updateOne(StudenthomeworkDTO.INSERT,studenthomeworkDTO,M10040WS);
				    		HomeworkhistoryDTO homeworkhistoryDTO=new HomeworkhistoryDTO();
				    		homeworkhistoryDTO.sethomeworkid(homeWorkId);
				    		homeworkhistoryDTO.setsenderid(sendId);
				    		homeworkhistoryDTO.setreceiverid(Integer.parseInt(userIdArray[i]));
				    		homeworkhistoryDTO.setfileid(fileId);
				    		homeworkhistoryDTO.setstate(0);
				    		updateDAO.updateOne(HomeworkhistoryDTO.INSERT,homeworkhistoryDTO,M10040WS);
				    		UsermessageDTO usermessageDTO =new UsermessageDTO();
				    		MstteacherDTO mstteacherDTO =new MstteacherDTO();
				    		mstteacherDTO.setusrid(sendId);
				    		MstteacherDTO teacher=queryDAO.queryForObject(MstteacherDTO.EQUAL_SEARCH, mstteacherDTO);
				    		usermessageDTO.setTitel("您有一条"+teacher.getname()+"布置的作业");
				    		usermessageDTO.setMessage("老师");
				    		usermessageDTO.setSenderId(sendId);
				    		usermessageDTO.setReceiveId(Integer.parseInt(userIdArray[i]));
				    		usermessageDTO.setSendDT(getCurrentDate());
				    		usermessageDTO.setSendTime(getCurrentTime());
				    		usermessageDTO.setReadflg(0);
				    		updateDAO.updateOne(UsermessageDTO.INSERT,usermessageDTO,M10040WS);
				    	}else{
				    		a++;
				    	}
		    		}
		    		if(a==userIdArray.length){
		    			createApplicationException(M10106WC);
		    		}
		    	}
		    }
	}
   private boolean checkHomeWorkIsExits(Integer userId,Integer homeWorkId){
	   StudenthomeworkDTO  studenthomeworkDTO =new StudenthomeworkDTO();
	   studenthomeworkDTO.setusrid(userId);
	   studenthomeworkDTO.sethomeworkid(homeWorkId);
	   if(queryDAO.count(StudenthomeworkDTO.COUNT, studenthomeworkDTO)>0){
		   return false;
	   }else{
		   return true;
	   }
   }
}

/*@Override
public void insertUserInfo(String email, String userName, String passWord) {
	checkUserInfo(email,userName);
	MstusrDTO mstusrDTO =new MstusrDTO(); 
	mstusrDTO.setemail(email);
	mstusrDTO.setusername(userName);
	mstusrDTO.setpassword(passWord);
	mstusrDTO.settype(0);
	mstusrDTO.setstate(0);
	mstusrDTO.setfirstloginflg(0);
	mstusrDTO.setlastdate(getCurrentDate());
	mstusrDTO.setlasttime(getCurrentTime());
	updateDAO.updateOne(MstusrDTO.INSERT,mstusrDTO,M10040WS);
	MststudentDTO mststudentDTO=new MststudentDTO();
    mststudentDTO.setusrid(mstusrDTO.getusrid());
    mststudentDTO.setlastdate(getCurrentDate());
    mststudentDTO.setlasttime(getCurrentTime());
    updateDAO.updateOne(MststudentDTO.INSERT,mststudentDTO,M10040WS);
    UUID uuid = UUID.randomUUID();
    String activeCode =uuid.toString();
    EmailactiveDTO emailactiveDTO=new EmailactiveDTO();
    emailactiveDTO.setactiveCode(activeCode);
    emailactiveDTO.setuserid(mstusrDTO.getusrid());
    updateDAO.updateOne(EmailactiveDTO.INSERT, emailactiveDTO, M10040WS);
    StringBuffer sb=new StringBuffer("点击下面链接激活账号,请尽快激活！</br>");
    sb.append("<a href=\"http://localhost:9090/NFE/emailActiveAction.do?&activeCode=");
    sb.append(activeCode);
    sb.append("\">http://localhost:9090/NFE/emailActiveAction.do?&activeCode=");
    sb.append(activeCode);
    sb.append("</a>");
    SendEmail sendEmail=new SendEmail();
    sendEmail.send(activeCode, sb.toString());
    sendEmail(email, sb.toString());
}

private void checkUserInfo(String email, String userName) {
	MstusrDTO mstusrDTO =new MstusrDTO(); 
	mstusrDTO.setemail(email);
	if(queryDAO.count(MstusrDTO.COUNT, mstusrDTO) > 0){
		createApplicationException(M10060WC);
	}else{
		mstusrDTO.setemail(null);
		mstusrDTO.setusername(userName);;
		if(queryDAO.count(MstusrDTO.COUNT, mstusrDTO) > 0){
			createApplicationException(M10070WC);
		}
	}
}*/


